package com.pirestupppai.widgets.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.pirestupppai.R;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.bean.LoginUser;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.utils.Utils;

import static com.pirestupppai.rxbus.RxBus.RXBUS_REFRESH_PROFILE;
import static com.pirestupppai.widgets.view.FollowButton.FollowState.FOLLOWING;
import static com.pirestupppai.widgets.view.FollowButton.FollowState.UNFOLLOW;

/**
 * 关注和取消关注按钮
 *
 * @author: xiaoluo
 * @date: 2017-03-09 14:28
 */
public class FollowButton extends android.support.v7.widget.AppCompatTextView {
    private Context mContext;

    public static final int REQUEST_UNFOLLOW = 0;
    public static final int REQUEST_FOLLOW = 1;

    private FeedItem mFeedItem;
    private TupaiUser mUser;
    private int mUid;
    private int mRelataion;
    private FollowState mState;
    private boolean isUser = false;  // 是否是用户界面
    private onResultListener mListener;

    public FollowButton(Context context) {
        super(context);
        mContext = context;
    }

    public FollowButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }


    private void initViews() {
        if (LoginUser.getInstance().getId() == mUid) {
            setVisibility(GONE);
            return;
        } else {
            setVisibility(VISIBLE);
        }

        if (mRelataion == 1 || mRelataion == 3) {
            mState = FOLLOWING;
        } else {
            mState = UNFOLLOW;
        }
        updateState(mState);
    }

    private void initListeners() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendFollowRequest();
            }
        });
    }

    /**
     * 发送关注或取消关注请求
     */
    private void sendFollowRequest() {
        setClickable(false);
        if (mState == FOLLOWING) {
            mState = UNFOLLOW;
            // 取消关注
            NetworkRequest.getInstance().deleteFollow(mUid, new TupaiSubscriber(mContext) {
                @Override
                protected void requestSuccess(Object o) {
                    updateState(mState);
                    setClickable(true);
                    Utils.showToast("取消关注成功");
                    LoginUser.getInstance().setFollowed_count(LoginUser.getInstance().getFollowed_count() - 1);
                    RxBus.getInstance().post(RXBUS_REFRESH_PROFILE, "");
                    if (mListener != null) {
                        mListener.onSucceed(REQUEST_UNFOLLOW);
                    }
                }

                @Override
                protected void requestError(String error) {
                    mState = FOLLOWING;
                    setClickable(true);
                    Utils.showToast("取消关注失败");
                }
            });
        } else {
            mState = FOLLOWING;
            // + 关注
            NetworkRequest.getInstance().createFollow(mUid,
                    new TupaiSubscriber(mContext) {
                        @Override
                        protected void requestSuccess(Object o) {
                            updateState(mState);
                            setClickable(true);
                            Utils.showToast("关注成功");
                            LoginUser.getInstance().setFollowed_count(LoginUser.getInstance().getFollowed_count() + 1);
                            RxBus.getInstance().post(RXBUS_REFRESH_PROFILE, "");
                            if (mListener != null) {
                                mListener.onSucceed(REQUEST_FOLLOW);
                            }
                        }

                        @Override
                        protected void requestError(String error) {
                            mState = UNFOLLOW;
                            updateState(mState);
                            setClickable(true);
                            Utils.showToast("关注失败");
                        }
                    });
        }
    }

    /**
     * 更新状态
     */
    private void updateState(FollowState state) {
        if (LoginUser.getInstance().getId() == mUid) {
            return;
        }

        setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
        if (state == UNFOLLOW) {
            setBackgroundResource(R.drawable.folliow);
            setText("");
            setTextColor(getResources().getColor(R.color.black));
            ObjectAnimator in = ObjectAnimator.ofFloat(this, "Alpha", 0.0f, 1.0f);
            in.setDuration(500);
            in.start();
        } else if (state == FOLLOWING) {
            if (isUser) {
                setText("");
                setTextColor(getResources().getColor(R.color.black));
                setBackgroundResource(R.drawable.folliowing);
                ObjectAnimator in = ObjectAnimator.ofFloat(this, "Alpha", 0.0f, 1.0f);
                in.setDuration(500);
                in.start();
            } else {
                setVisibility(GONE);
            }
        }
    }

    /**
     * 设置PhotoItem
     */
    public void setFeedItem(FeedItem item) {
        mFeedItem = item;
        mUid = item.getUid();
        mRelataion = item.getUser().getRelation();
        isUser = false;
        initViews();
        initListeners();
    }

    /**
     * 用户页设置User
     */
    public void setUser(TupaiUser user) {
        mUid = user.getId();
        mRelataion = user.getRelation();
        isUser = true;
        initViews();
        initListeners();
    }

    /**
     * 设置请求成功回调
     */
    public void setOnResultListener(onResultListener listener) {
        mListener = listener;
    }

    public enum FollowState {
        UNFOLLOW, FOLLOWING
    }

    public interface onResultListener{
        void onSucceed(int type);
    }
}
